import 'package:flutter/material.dart';
import 'package:qr_code_tools/widgets/scan_to_file.dart';
import 'scan_to_text.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key, required this.title});

  final String title;

  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int currentNavIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          title: Text(widget.title),
          centerTitle: true,
          // toolbarHeight: 0,
        ),
        body: Center(
            child: SizedBox(
                width: MediaQuery.of(context).size.width,
                child: _buildBody(context))),
        bottomNavigationBar: _buildNavigationBar(context));
  }

  Widget _buildNavigationBar(BuildContext context) {
    return BottomNavigationBar(
      backgroundColor: Theme.of(context).colorScheme.onInverseSurface,
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.textsms),
          label: '文本',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.file_copy),
          label: '文件',
        ),
      ],
      currentIndex: currentNavIndex,
      selectedItemColor: Theme.of(context).colorScheme.primary,
      onTap: (index) => setState(() {
        currentNavIndex = index;
      }),
    );
  }

  Widget _buildBody(BuildContext context){
    switch (currentNavIndex){
      case 0: return const ScanToText();
      case 1: return const ScanToFile();
    }
    return const Text("错误页面索引");
  }
}
